package com.he.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.domain.dto.*;
import com.he.domain.vo.CommonLinkVO;
import com.he.domain.vo.LinkBackVO;
import com.he.domain.vo.FriendLinkVO;
import com.he.entity.Link;
import com.he.enums.LinkEnum;
import com.he.mapper.LinkMapper;
import com.he.service.LinkService;
import com.he.utils.BeanCopyUtils;
import com.he.utils.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/15 14:48
 * @Desc 链接服务实现类
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Resource
    private LinkMapper linkMapper;

    @Override
    public List<FriendLinkVO> getFriendLinkVOList() {
       return linkMapper.getFriendLinkVOList();
    }

    @Override
    public void addFriendLink(FriendLinkDTO friendLinkDTO) {
        Link link = BeanCopyUtils.copyBean(friendLinkDTO, Link.class);
        link.setType(LinkEnum.FRIEND.getType());
        linkMapper.insert(link);
    }

    @Override
    public void deleteFriendLink(Integer id) {
           Assert.notNull(id,"id不能为空");
           linkMapper.deleteById(id);
    }

    @Override
    public void updateFriendLink(FriendLinkDTO friendLinkDTO) {
        Assert.notNull(friendLinkDTO.getId(),"id不能为空");
        Link link = BeanCopyUtils.copyBean(friendLinkDTO, Link.class);
        updateById(link);
    }

    @Override
    public List<CommonLinkVO> getQuickLinkList() {
        return linkMapper.getCommonLinkList(LinkEnum.QUICK.getType());
    }

    @Override
    public List<CommonLinkVO> getTechniqueLinkList() {
        return linkMapper.getCommonLinkList(LinkEnum.TECHNIQUE.getType());
    }

    @Override
    public void addCommonLink(CommonLinkDTO commonLinkDTO) {
        Link link = BeanCopyUtils.copyBean(commonLinkDTO, Link.class);
        linkMapper.insert(link);
    }

    @Override
    public PageResult<LinkBackVO> getLinkBackVOList(ConditionDTO condition) {
        // 查询链接
        Long count = linkMapper.selectCount(new LambdaQueryWrapper<Link>()
                .like(StringUtils.hasText(condition.getKeyword()), Link::getName, condition.getKeyword())
                .eq(Objects.nonNull(condition.getTypeId()), Link::getType, condition.getTypeId())
        );
        if (count == 0) {
            return new PageResult<>();
        }
        // 查询后台链接列表
        List<LinkBackVO> linkBackVOList = linkMapper.selectLinkBackVOList(PageUtils.getLimit(), PageUtils.getSize(), condition.getKeyword(), condition.getTypeId());
        return new PageResult<>(linkBackVOList, count);
    }

    @Override
    public void addLink(LinkDTO linkDTO) {
        Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
        baseMapper.insert(link);
    }

    @Override
    public void updateLink(LinkDTO linkDTO) {
        // 新友链
        Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
        // 更新友链
        baseMapper.updateById(link);
    }


}
